package com.ruoyi.kjcg.jxdj.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.kjcg.jxdj.domain.Jxdj;
import com.ruoyi.kjcg.jxdj.domain.JxdjFj;
import com.ruoyi.kjcg.jxdj.mapper.JxdjMapper;
import com.ruoyi.kjcg.jxdj.service.IJxdjService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 奖项登记Service业务层处理
 *
 * @author huacai
 * @date 2024-11-07
 */
@Service
public class JxdjServiceImpl implements IJxdjService {
    @Resource
    private JxdjMapper jxdjMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询奖项登记
     *
     * @param jxdjId 奖项登记主键
     * @return 奖项登记
     */
    @Override
    public Jxdj selectJxdjByJxdjId(String jxdjId) {
        return jxdjMapper.selectJxdjByJxdjId(jxdjId);
    }

    /**
     * 查询奖项登记列表
     *
     * @param jxdj 奖项登记
     * @return 奖项登记
     */
    @Override
    public List<Jxdj> selectJxdjList(Jxdj jxdj) {
        return jxdjMapper.selectJxdjList(jxdj);
    }

    /**
     * 新增奖项登记
     *
     * @param jxdj 奖项登记
     * @return 结果
     */
    @Transactional
    @Override
    public int insertJxdj(Jxdj jxdj) {
        jxdj.setCreateTime(DateUtils.getNowDate());
        int rows = jxdjMapper.insertJxdj(jxdj);
        insertJxdjFj(jxdj);
        return rows;
    }

    /**
     * 批量新增奖项登记
     *
     * @param jxdjs 奖项登记List
     * @return 结果
     */
    @Override
    public int batchInsertJxdj(List<Jxdj> jxdjs) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(jxdjs)) {
            try {
                for (int i = 0; i < jxdjs.size(); i++) {
                    int row = jxdjMapper.insertJxdj(jxdjs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == jxdjs.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改奖项登记
     *
     * @param jxdj 奖项登记
     * @return 结果
     */
    @Transactional
    @Override
    public int updateJxdj(Jxdj jxdj) {
        jxdj.setUpdateTime(DateUtils.getNowDate());
//        jxdjMapper.deleteJxdjFjByJxdjId(jxdj.getJxdjId());
        insertJxdjFj(jxdj);
        return jxdjMapper.updateJxdj(jxdj);
    }

    /**
     * 批量删除奖项登记
     *
     * @param jxdjIds 需要删除的奖项登记主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteJxdjByJxdjIds(String[] jxdjIds) {
        jxdjMapper.deleteJxdjFjByJxdjIds(jxdjIds);
        return jxdjMapper.deleteJxdjByJxdjIds(jxdjIds);
    }

    /**
     * 删除奖项登记信息
     *
     * @param jxdjId 奖项登记主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteJxdjByJxdjId(String jxdjId) {
        jxdjMapper.deleteJxdjFjByJxdjId(jxdjId);
        return jxdjMapper.deleteJxdjByJxdjId(jxdjId);
    }

    /**
     * 新增附件
     * @param jxdjFj
     * @return
     */
    @Override
    public int insertJxdjFjByJxdjId(JxdjFj jxdjFj) {
        return jxdjMapper.insertJxdjFjByJxdjId(jxdjFj);
    }

    /**
     * 根据ID查询附件列表
     * @param jxdjFj
     * @return
     */
    @Override
    public List<JxdjFj> selectJxdjFjList(JxdjFj jxdjFj) {
        return jxdjMapper.selectJxdjFjList(jxdjFj);
    }

    /**
     * 删除奖项登记附件
     * @param fjIds
     * @return
     */
    @Override
    public int deleteJxdjFjByFjIds(Integer[] fjIds) {
        return jxdjMapper.deleteJxdjFjByFjIds(fjIds);
    }

    /**
     * 新增奖项登记附件信息
     *
     * @param jxdj 奖项登记对象
     */
    public void insertJxdjFj(Jxdj jxdj) {
        List<JxdjFj> jxdjFjList = jxdj.getJxdjFjList();
        String jxdjId = jxdj.getJxdjId();
        if (StringUtils.isNotNull(jxdjFjList)) {
            List<JxdjFj> list = new ArrayList<JxdjFj>();
            for (JxdjFj jxdjFj : jxdjFjList) {
                jxdjFj.setJxdjId(jxdjId);
                list.add(jxdjFj);
            }
//            if (list.size() > 0) {
//                jxdjMapper.batchJxdjFj(list);
//            }
        }
    }
}
